import Vue from 'vue'
import Router from 'vue-router'
// 组件
import Home from '@/components/home/Home'
import Login from '@/components/login/Login'
// 按需加载
const Users = () => import('@/components/users/Users')
const Roles = () => import('@/components/roles/Roles.vue')
const Rights = () => import('@/components/rights/Rights')
const Categories = () => import('@/components/categories/categories.vue')
const Goods = () => import(/* webpackChunkName: 'goods' */'@/components/goods/Goods.vue')
const GoodsAdd = () => import(/* webpackChunkName: 'goods' */'@/components/goods-add/Goods-add.vue')
// import Users from '@/components/users/Users'
// import Roles from '@/components/roles/Roles.vue'
// import Rights from '@/components/rights/Rights'
// import Categories from '@/components/categories/categories.vue'
// import Goods from '@/components/goods/Goods.vue'
// import GoodsAdd from '@/components/goods-add/Goods-add.vue'
const NotFound = () => import('@/components/not-found/NotFound.vue')
Vue.use(Router)
// 路由实例
const router = new Router({
  routes: [
    { path: '/', redirect: '/home' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      children: [
        { path: '/users', component: Users },
        { path: '/roles', component: Roles },
        { path: '/rights', component: Rights },
        { path: '/categories', component: Categories },
        { path: '/goods/:id?', component: Goods },
        { path: '/goods-add', component: GoodsAdd }
      ]
    },
    { path: '*', component: NotFound }
  ]
})
// 全局守卫
router.beforeEach((to, from, next) => {
  if (to.fullPath === '/login') {
    next()
    return
  }
  if (localStorage.getItem('token')) {
    next()
  } else {
    next('/login')
  }
})

export default router
